package com.atguigu.eduorder.service.impl;

import com.atguigu.commonutils.OrderNoUtil;
import com.atguigu.commonutils.RandomUtil;
import com.atguigu.commonutils.ordervo.CourseWebOrder;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.eduorder.client.EduClient;
import com.atguigu.eduorder.client.UcenterClient;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.mapper.OrderMapper;
import com.atguigu.eduorder.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.UUID;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author zhaolie
 * @since 2020-12-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private EduClient eduClient;

    @Resource
    private UcenterClient ucenterClient;

    @Override
    public String createOrder(String courseId, String userId) {
        // 远程调用用户id获取用户信息
        UcenterMemberOrder userInfo = ucenterClient.getUserInfo(userId);
        // 远程调用课程id获取课程信息
        CourseWebOrder courseInfo = eduClient.getCourseInfoOrder(courseId);

        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("member_id", userId);
        orderQueryWrapper.eq("course_id", courseId);
        Order selectOne = baseMapper.selectOne(orderQueryWrapper);
        if (selectOne != null) {
            return selectOne.getOrderNo();
        }
        Order order = new Order();
        String orderNo = OrderNoUtil.getOrderNo();
        order.setOrderNo(orderNo);
        order.setNickname(userInfo.getNickname());
        order.setMemberId(userInfo.getId());
        order.setMobile(userInfo.getMobile());
        order.setCourseId(courseInfo.getId());
        order.setCourseTitle(courseInfo.getTitle());
        order.setCourseCover(courseInfo.getCover());
        order.setTeacherName(courseInfo.getTeacherName());
        order.setTotalFee(courseInfo.getPrice());
        order.setStatus(0);
        order.setPayType(1);
        System.out.println(order);
        baseMapper.insert(order);
        return order.getOrderNo();
    }

    @Override
    public Order getOrderInfo(String orderNo) {
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq("order_no", orderNo);
        Order order = baseMapper.selectOne(orderQueryWrapper);
        return order;
    }
}
